<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>权限列表</title>
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
    <link rel="stylesheet" th:href="@{/font-awesome-4.7.0/css/font-awesome.min.css}">
    <!--tree插件-->
    <link rel="stylesheet" th:href="@{/layuiadmin/extend/treetable/treetable.css}">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="addParent">添加父节点</button>
            </div>
            <table id="table1" class="layui-table" lay-filter="table1"></table>

        </div>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script th:src="@{/layuiadmin/extend/treetable/treetable.js}"></script>

<script type="text/html" id="oper-col">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="add">添加</a>
</script>

<script>
    layui.config({
        base: '/layuiadmin/extend/'
    }).extend({
        treetable: 'treetable/treetable'
    }).use(['treetable','table','jquery'], function () {
        var treetable = layui.treetable;
        var table = layui.table;
        var $ = layui.jquery;

        // 渲染表格
        treetable.render({
            treeColIndex: 2,          // treetable新增参数
            treeSpid: -1,             // treetable新增参数
            treeIdName: 'd_id',       // treetable新增参数
            treePidName: 'd_pid',     // treetable新增参数
            treeDefaultClose: true,   // treetable新增参数
            treeLinkage: true,        // treetable新增参数
            elem: '#table1',
            url: '/badminton/permissionList',
            cols: [
                [
                    {type: 'numbers'},
                    {field: 'name', title: '名称'},
                    {field: 'url', title: '路径'},
                    {templet:'#oper-col',title:'操作'}
                ]
            ]
        });

        table.on('tool(table1)',function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    del(data,obj,index);
                });
            }else if(layEvent === 'edit'){
                layer.open({
                    type: 2,
                    title: '修改权限',
                    skin: 'layui-layer-demo', //加上边框
                    area: ['500px', '450px'], //宽高
                    content: '/badminton/addPermission?type=edit&id='+data.id
                });
            }else if(layEvent === 'add'){
                if(data.pid == -1){
                    layer.open({
                        type: 2,
                        title: '添加权限',
                        skin: 'layui-layer-demo', //加上边框
                        area: ['500px', '450px'], //宽高
                        content: '/badminton/addPermission?type=add&id='+data.id
                    });
                }else{
                    layer.msg("只能创建二级目录");
                }
            }
        })

        function del(data,obj,index){
            var jsonObj = {};
            jsonObj["ids[0]"] = data.id;
            $.ajax({
                url:'/badminton/delPermission',
                data:jsonObj,
                dataType:'json',
                type:'post',
                success:function (data) {
                    if (data.state){
                        obj.del(); //删除对应行（tr）的DOM结构
                        layer.close(index);
                    }else{
                        layer.msg(data.message);
                    }
                }
            })
        }

        var active = {
            addParent:function () {
                layer.open({
                    type: 2,
                    title: '添加权限',
                    skin: 'layui-layer-demo', //加上边框
                    area: ['500px', '450px'], //宽高
                    content: '/badminton/addPermission?type=addParent'
                });
            }
        }

        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });





</script>
</body>
</html>
